$(document).ready(function(){
	var sdate;
	while(sdate==null||sdate==""){
		sdate=$("#sdate").val();
	}
	var now;
	var xhr = new XMLHttpRequest();  
	ajax()
	  function ajax(option){
	    var xhr = null;
	    if(window.XMLHttpRequest){
	      xhr = new window.XMLHttpRequest();
	    }else{ // ie
	      xhr = new ActiveObject("Microsoft")
	    }
	    // 通过get的方式请求当前文件
	    xhr.open("get","/");
	    xhr.send(null);
	    // 监听请求状态变化
	    xhr.onreadystatechange = function(){
	      if(xhr.readyState===2){
	        // 获取响应头里的时间戳
	        var time = xhr.getResponseHeader('Date');
	        now=new Date(time);
	        //now = new Date();
	        var stime=Number(sdate);
			var nowtime=Number(now.getTime());
			console.log(nowtime+""+stime);
			var newtime=stime-nowtime;
			var ss=foreach(newtime);
	      }
	    }
	  }
	$("#closeLogin").click(function(){
		$("#login").css("display","none");
	});
	$("#submitform").click(function(){
		var form=$("#loginform").serializeArray();
		$.post("/mall/user/loginAjax.action",form,function(data){
			if(data.result==1){
				var killid=$("#killid").val();
				var date=new Date();
				window.location.href="/mall/seckill/jsp/"+killid+".jsp?time="+date.getTime();
			}else{
				$("#error").html(data.result);
			}
		});
		return false;
	});
	$("#killStart").click(function(){
		var userid=$("#userid").val();
		if(userid==""||userid==null){
			$("#login").css("display","block");
			return false
		}
		
	});	
		
});
function foreach(time){
	console.log(time);
	//倒计时算法
	var num=time-1000;
	var hspan=$("#HH");
	var mspan=$("#MM");
	var sspan=$("#SS");
	var se=setInterval(function(){
		var htime=num/3600000;
		var harr=htime.toString().split(".");
		var HH=harr[0];
		var mmtime=60*Number("0."+harr[1]);
		var mmarr=mmtime.toString().split(".");
		var mm=mmarr[0];
		var sstime=60*Number("0."+mmarr[1]);
		var ss=Math.round(sstime);
		num=num-1000;
		if(isNaN(mm)||mm==60){
			mm=0
		}
		if(isNaN(ss)||ss==60){
			ss=0;
		}
		hspan.html(HH);
		mspan.html(mm);
		sspan.html(ss);
	},999);
	var se2=setInterval(function(){
		if(num<=500){
			var path=$("#path").val();
			$("#timediv").css("text-align","center");
			$("#timediv").css("font-size","18px");
			$("#timediv").css("color","red");
			$("#timediv").html("秒杀开始!");
			$("#killStart").html("立即秒杀");
			$("#killStart").css({"cursor":"pointer","background":"red","color":"#fff","border-color":"#EE5C42"});
			var id=$("#killid").val();
			$("#killStart").attr("href",path+"SeckillController/seckill.action?id="+id);
			changesub();	
			clearInterval(se);
			clearInterval(se2);
		}
	},100);
}